<template>
  <view v-if="show" class="global-loading">
    <view class="loading-wrapper" :class="[`loading-${type}`, `loading-${size}`]">
      <!-- 旋转圆点样式 -->
      <view v-if="type === 'spinner'" class="loading-spinner">
        <view class="spinner-dot" v-for="(item, index) in 8" :key="index" :style="{ animationDelay: index * 0.1 + 's' }"></view>
      </view>
      
      <!-- 脉冲样式 -->
      <view v-else-if="type === 'pulse'" class="loading-pulse">
        <view class="pulse-circle"></view>
      </view>
      
      <!-- 波浪样式 -->
      <view v-else-if="type === 'wave'" class="loading-wave">
        <view class="wave-bar" v-for="(item, index) in 5" :key="index" :style="{ animationDelay: index * 0.1 + 's' }"></view>
      </view>
      
      <!-- 默认旋转样式 -->
      <view v-else class="loading-default">
        <view class="loading-icon"></view>
      </view>
      
      <!-- 加载文字 -->
      <text v-if="text" class="loading-text">{{ text }}</text>
    </view>
  </view>
</template>

<script>
import loadingManager from '@/utils/loading.js'

export default {
  name: 'GlobalLoading',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      text: '加载中...',
      type: 'default',
      size: 'medium'
    }
  },
  mounted() {
    // 监听loading状态变化
    loadingManager.addListener(this.handleLoadingChange)
    // 初始化状态
    const state = loadingManager.getState()
    this.text = state.text
    this.type = state.type
    this.size = state.size
  },
  beforeDestroy() {
    // 移除监听器
    loadingManager.removeListener(this.handleLoadingChange)
  },
  methods: {
    handleLoadingChange(state) {
      this.text = state.text
      this.type = state.type
      this.size = state.size
    }
  }
}
</script>

<style lang="scss" scoped>
.global-loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.loading-wrapper {
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
  
  // 大小配置
  &.loading-small {
    padding: 40rpx;
    
    .loading-icon {
      width: 40rpx;
      height: 40rpx;
    }
    
    .spinner-dot {
      width: 8rpx;
      height: 8rpx;
    }
    
    .pulse-circle {
      width: 40rpx;
      height: 40rpx;
    }
    
    .wave-bar {
      width: 6rpx;
      height: 30rpx;
    }
  }
  
  &.loading-medium {
    padding: 60rpx;
    
    .loading-icon {
      width: 60rpx;
      height: 60rpx;
    }
    
    .spinner-dot {
      width: 12rpx;
      height: 12rpx;
    }
    
    .pulse-circle {
      width: 60rpx;
      height: 60rpx;
    }
    
    .wave-bar {
      width: 8rpx;
      height: 40rpx;
    }
  }
  
  &.loading-large {
    padding: 80rpx;
    
    .loading-icon {
      width: 80rpx;
      height: 80rpx;
    }
    
    .spinner-dot {
      width: 16rpx;
      height: 16rpx;
    }
    
    .pulse-circle {
      width: 80rpx;
      height: 80rpx;
    }
    
    .wave-bar {
      width: 10rpx;
      height: 50rpx;
    }
  }
}

// 默认旋转样式
.loading-default {
  .loading-icon {
    border: 4rpx solid #f3f3f3;
    border-top: 4rpx solid #007aff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

// 圆点旋转样式
.loading-spinner {
  position: relative;
  width: 60rpx;
  height: 60rpx;
  
  .spinner-dot {
    position: absolute;
    width: 12rpx;
    height: 12rpx;
    border-radius: 50%;
    background-color: #007aff;
    animation: spinner 1.2s linear infinite;
    
    &:nth-child(1) { top: 0; left: 50%; transform: translateX(-50%); }
    &:nth-child(2) { top: 8rpx; right: 8rpx; }
    &:nth-child(3) { top: 50%; right: 0; transform: translateY(-50%); }
    &:nth-child(4) { bottom: 8rpx; right: 8rpx; }
    &:nth-child(5) { bottom: 0; left: 50%; transform: translateX(-50%); }
    &:nth-child(6) { bottom: 8rpx; left: 8rpx; }
    &:nth-child(7) { top: 50%; left: 0; transform: translateY(-50%); }
    &:nth-child(8) { top: 8rpx; left: 8rpx; }
  }
}

// 脉冲样式
.loading-pulse {
  .pulse-circle {
    width: 60rpx;
    height: 60rpx;
    border-radius: 50%;
    background-color: #007aff;
    animation: pulse 1.5s ease-in-out infinite;
  }
}

// 波浪样式
.loading-wave {
  display: flex;
  align-items: center;
  gap: 6rpx;
  
  .wave-bar {
    width: 8rpx;
    height: 40rpx;
    background-color: #007aff;
    border-radius: 4rpx;
    animation: wave 1.2s ease-in-out infinite;
  }
}

// 加载文字
.loading-text {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: #666666;
}

// 动画定义
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes spinner {
  0% { opacity: 1; }
  100% { opacity: 0.3; }
}

@keyframes pulse {
  0%, 100% { 
    transform: scale(1);
    opacity: 1;
  }
  50% { 
    transform: scale(1.2);
    opacity: 0.7;
  }
}

@keyframes wave {
  0%, 40%, 100% { 
    transform: scaleY(0.4);
  }
  20% { 
    transform: scaleY(1);
  }
}
</style> 